package com.yzr.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * @description
 * @author: yzr
 * @date: 2021/10/28 18:30
 **/
@Slf4j
public class JacobUtil {
    private static final int WDFO_RMATPDF = 17;
    private static final int XLTYPE_PDF = 0;

    public static void main(String[] args) throws IOException {
        /*log.info("=====================1MB-》wps,word->pdf 开始====================");
        long startTime0 = System.currentTimeMillis();
        wordToPdf("D:\\download\\zookeeper安装文档.docx", "D:\\wps\\1mbWord.pdf");
        long endTime0 = System.currentTimeMillis();
        DecimalFormat df0 = new DecimalFormat("0.000");
        log.debug("10MB=>>>" + df0.format((float) (endTime0 - startTime0) / 1000) + "s");

        log.info("=====================5MB-》wps,xlsx->pdf 开始====================");
        long startTime1 = System.currentTimeMillis();
        excelToPdf("D:\\wps\\5mb.xlsx", "D:\\wps\\5mb.pdf");
        long endTime1 = System.currentTimeMillis();
        DecimalFormat df1 = new DecimalFormat("0.000");
        log.debug("10MB=>>>" + df1.format((float) (endTime1 - startTime1) / 1000) + "s");

        log.info("=====================10MB-》wps,xlsx->pdf 开始====================");
        long startTime2 = System.currentTimeMillis();
        excelToPdf("D:\\wps\\10mb.xlsx", "D:\\wps\\10mb.pdf");
        long endTime2 = System.currentTimeMillis();
        DecimalFormat df2 = new DecimalFormat("0.000");
        log.debug("10MB=>>>" + df2.format((float) (endTime2 - startTime2) / 1000) + "s");*/
        File file = new File("D:\\download\\zookeeper安23432432432432234423sdssd装文档.docx");
        Files.write(file.toPath(), new byte[]{1});

    }

    /**
     * word转换成PDF
     *
     * @param wordPath 文件路径
     * @param pdfPath  文件路径
     * @return 成功失败
     */
    private static boolean wordToPdf(String wordPath, String pdfPath) {
        ActiveXComponent activeXComponent = new ActiveXComponent("Word.Application");
        activeXComponent.setProperty("Visible", new Variant(false));
        Dispatch docs = Dispatch.get(activeXComponent, "Documents").toDispatch();
        Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{wordPath, new Variant(false), new Variant(true)}, new int[1]).toDispatch();

        Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{pdfPath, new Variant(WDFO_RMATPDF)}, new int[1]);

        Dispatch.call(doc, "Close", false);
        activeXComponent.invoke("Quit");
        return true;
    }

    /**
     * excel转换成PDF
     *
     * @param inputPath 文件路径
     * @param pdfPath   文件路径
     * @return 成功失败
     */
    private static boolean excelToPdf(String inputPath, String pdfPath) {
        ActiveXComponent activeXComponent = new ActiveXComponent("Excel.Application");
        activeXComponent.setProperty("Visible", false);

        Dispatch excels = activeXComponent.getProperty("Workbooks").toDispatch();
        Dispatch excel = Dispatch.call(excels, "Open", inputPath, false, true).toDispatch();
        Dispatch.call(excel, "ExportAsFixedFormat", XLTYPE_PDF, pdfPath);
        Dispatch.call(excel, "Close", false);
        activeXComponent.invoke("Quit");
        return true;
    }

    /**
     * ppt转换成PDF
     *
     * @param inputPath 文件路径
     * @param pdfPath   文件路径
     * @return 成功失败
     */
    private static boolean pptToPdf(String inputPath, String pdfPath) {
        //		ComThread.InitSTA();
        ActiveXComponent activeXComponent = new ActiveXComponent("PowerPoint.Application");
//        activeXComponent.setProperty("Visible", new Variant(false));
        Dispatch ppts = activeXComponent.getProperty("Presentations").toDispatch();

        Dispatch ppt = Dispatch.call(ppts, "Open", inputPath, false, // ReadOnly
                true, // Untitled指定文件是否有标题
                true// WithWindow指定文件是否可见
        ).toDispatch();
//        Dispatch.callN(ppt, "SaveAs", new Variant(pdfPath));
        Dispatch.callN(ppt, "SaveAs", new Variant(pdfPath), 32);
        Dispatch.call(ppt, "Close");
        activeXComponent.invoke("Quit");
        return true;
    }
}
